<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>pgmpy.factors.continuous.LinearGaussianCPD &#8212; pgmpy 0.1.2 documentation</title>
    
    <link rel="stylesheet" href="../../../../_static/sphinxdoc.css" type="text/css" />
    <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../../',
        VERSION:     '0.1.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true,
        SOURCELINK_SUFFIX: '.txt'
      };
    </script>
    <script type="text/javascript" src="../../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../../_static/doctools.js"></script>
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" /> 
  </head>
  <body role="document">
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../../index.html">pgmpy 0.1.2 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../../index.html" accesskey="U">Module code</a> &#187;</li> 
      </ul>
    </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../../../../index.html">
              <img class="logo" src="../../../../_static/logo.png" alt="Logo"/>
            </a></p>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="../../../../search.html" method="get">
      <div><input type="text" name="q" /></div>
      <div><input type="submit" value="Go" /></div>
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <h1>Source code for pgmpy.factors.continuous.LinearGaussianCPD</h1><div class="highlight"><pre>
<span></span><span class="c1"># -*- coding: utf-8 -*-</span>

<span class="kn">from</span> <span class="nn">__future__</span> <span class="k">import</span> <span class="n">division</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">from</span> <span class="nn">scipy.stats</span> <span class="k">import</span> <span class="n">multivariate_normal</span>

<span class="kn">from</span> <span class="nn">pgmpy.factors.base</span> <span class="k">import</span> <span class="n">BaseFactor</span>


<div class="viewcode-block" id="LinearGaussianCPD"><a class="viewcode-back" href="../../../../factors.html#pgmpy.factors.continuous.LinearGaussianCPD.LinearGaussianCPD">[docs]</a><span class="k">class</span> <span class="nc">LinearGaussianCPD</span><span class="p">(</span><span class="n">BaseFactor</span><span class="p">):</span>
    <span class="sd">u&quot;&quot;&quot;</span>
<span class="sd">    For, X -&gt; Y the Linear Gaussian model assumes that the mean</span>
<span class="sd">    of Y is a linear function of mean of X and the variance of Y does</span>
<span class="sd">    not depend on X.</span>

<span class="sd">    For example,</span>
<span class="sd">    p(Y|X) = N(-2x + 0.9 ; 1)</span>
<span class="sd">    Here, x is the mean of the variable X.</span>

<span class="sd">    Let Y be a continuous variable with continuous parents</span>
<span class="sd">    X1 ............ Xk . We say that Y has a linear Gaussian CPD</span>
<span class="sd">    if there are parameters β0,.........βk and σ2 such that,</span>

<span class="sd">    p(Y |x1.......xk) = N(β0 + x1*β1 + ......... + xk*βk ; σ2)</span>

<span class="sd">    In vector notation,</span>

<span class="sd">    p(Y |x) = N(β0 + β.T * x ; σ2)</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">variable</span><span class="p">,</span> <span class="n">beta</span><span class="p">,</span> <span class="n">variance</span><span class="p">,</span> <span class="n">evidence</span><span class="o">=</span><span class="p">[]):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>

<span class="sd">        variable: any hashable python object</span>
<span class="sd">            The variable whose CPD is defined.</span>

<span class="sd">        beta: iterable of int or float</span>
<span class="sd">            An iterable representing the coefficient vector of the linear equation.</span>
<span class="sd">            The first term represents the constant term in the linear equation.</span>

<span class="sd">        variance: int, float</span>
<span class="sd">            The variance of the variable defined.</span>

<span class="sd">        evidence: iterable of any hashabale python objects</span>
<span class="sd">            An iterable of the parents of the variable. None if there are no parents.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>

<span class="sd">        # For P(Y| X1, X2, X3) = N(-2x1 + 3x2 + 7x3 + 0.2; 9.6)</span>

<span class="sd">        &gt;&gt;&gt; cpd = LinearGaussianCPD(&#39;Y&#39;,  [0.2, -2, 3, 7], 9.6, [&#39;X1&#39;, &#39;X2&#39;, &#39;X3&#39;])</span>
<span class="sd">        &gt;&gt;&gt; cpd.variable</span>
<span class="sd">        &#39;Y&#39;</span>
<span class="sd">        &gt;&gt;&gt; cpd.variance</span>
<span class="sd">        9.6</span>
<span class="sd">        &gt;&gt;&gt; cpd.evidence</span>
<span class="sd">        [&#39;x1&#39;, &#39;x2&#39;, &#39;x3&#39;]</span>
<span class="sd">        &gt;&gt;&gt; cpd.beta_vector</span>
<span class="sd">        [0.2, -2, 3, 7]</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">variable</span> <span class="o">=</span> <span class="n">variable</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">beta</span> <span class="o">=</span> <span class="n">beta</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">beta_0</span> <span class="o">=</span> <span class="n">beta</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">variance</span> <span class="o">=</span> <span class="n">variance</span>

        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">evidence</span><span class="p">)</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="n">beta</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;The number of variables in evidence must be one less than the &quot;</span>
                             <span class="s2">&quot;length of the beta vector.&quot;</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">evidence</span> <span class="o">=</span> <span class="n">evidence</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">beta_vector</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="n">beta</span><span class="p">[</span><span class="mi">1</span><span class="p">:])</span>

        <span class="n">variables</span> <span class="o">=</span> <span class="p">[</span><span class="n">variable</span><span class="p">]</span> <span class="o">+</span> <span class="n">evidence</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">LinearGaussianCPD</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="n">variables</span><span class="p">,</span> <span class="n">pdf</span><span class="o">=</span><span class="s1">&#39;gaussian&#39;</span><span class="p">,</span>
                                                <span class="n">mean</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">beta_vector</span><span class="p">,</span>
                                                <span class="n">covariance</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">variance</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">pdf</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>

        <span class="k">def</span> <span class="nf">_pdf</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">):</span>
            <span class="c1"># The first element of args is the value of the variable on which CPD is defined</span>
            <span class="c1"># and the rest of the elements give the mean values of the parent variables.</span>
            <span class="n">mean</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">([</span><span class="n">arg</span> <span class="o">*</span> <span class="n">coeff</span> <span class="k">for</span> <span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="n">coeff</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">args</span><span class="p">[</span><span class="mi">1</span><span class="p">:],</span> <span class="bp">self</span><span class="o">.</span><span class="n">beta_vector</span><span class="p">)])</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">beta_0</span>
            <span class="k">return</span> <span class="n">multivariate_normal</span><span class="o">.</span><span class="n">pdf</span><span class="p">(</span><span class="n">args</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">mean</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="bp">self</span><span class="o">.</span><span class="n">variance</span><span class="p">]]))</span>

        <span class="k">return</span> <span class="n">_pdf</span>

<div class="viewcode-block" id="LinearGaussianCPD.copy"><a class="viewcode-back" href="../../../../factors.html#pgmpy.factors.continuous.LinearGaussianCPD.LinearGaussianCPD.copy">[docs]</a>    <span class="k">def</span> <span class="nf">copy</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns a copy of the distribution.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        LinearGaussianCPD: copy of the distribution</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.continuous import LinearGaussianCPD</span>
<span class="sd">        &gt;&gt;&gt; cpd = LinearGaussianCPD(&#39;Y&#39;,  [0.2, -2, 3, 7], 9.6, [&#39;X1&#39;, &#39;X2&#39;, &#39;X3&#39;])</span>
<span class="sd">        &gt;&gt;&gt; copy_cpd = cpd.copy()</span>
<span class="sd">        &gt;&gt;&gt; copy_cpd.variable</span>
<span class="sd">        &#39;Y&#39;</span>
<span class="sd">        &gt;&gt;&gt; copy_cpd.evidence</span>
<span class="sd">        [&#39;X1&#39;, &#39;X2&#39;, &#39;X3&#39;]</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">copy_cpd</span> <span class="o">=</span> <span class="n">LinearGaussianCPD</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">variable</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">beta</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">variance</span><span class="p">,</span>
                                     <span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">evidence</span><span class="p">))</span>

        <span class="k">return</span> <span class="n">copy_cpd</span></div>

    <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">evidence</span> <span class="ow">and</span> <span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">beta_vector</span><span class="p">):</span>
            <span class="c1"># P(Y| X1, X2, X3) = N(-2*X1_mu + 3*X2_mu + 7*X3_mu; 0.2)</span>
            <span class="n">rep_str</span> <span class="o">=</span> <span class="s2">&quot;P(</span><span class="si">{node}</span><span class="s2"> | </span><span class="si">{parents}</span><span class="s2">) = N(</span><span class="si">{mu}</span><span class="s2"> + </span><span class="si">{b_0}</span><span class="s2">; </span><span class="si">{sigma}</span><span class="s2">)&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                <span class="n">node</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">variable</span><span class="p">),</span>
                <span class="n">parents</span><span class="o">=</span><span class="s1">&#39;, &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="nb">str</span><span class="p">(</span><span class="n">var</span><span class="p">)</span> <span class="k">for</span> <span class="n">var</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">evidence</span><span class="p">]),</span>
                <span class="n">mu</span><span class="o">=</span><span class="s2">&quot; + &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="s2">&quot;</span><span class="si">{coeff}</span><span class="s2">*</span><span class="si">{parent}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                    <span class="n">coeff</span><span class="o">=</span><span class="n">coeff</span><span class="p">,</span> <span class="n">parent</span><span class="o">=</span><span class="n">parent</span><span class="p">)</span> <span class="k">for</span> <span class="n">coeff</span><span class="p">,</span> <span class="n">parent</span> <span class="ow">in</span>
                                <span class="nb">zip</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">beta_vector</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">evidence</span><span class="p">)]),</span>
                <span class="n">b_0</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">beta_0</span><span class="p">),</span>
                <span class="n">sigma</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">variance</span><span class="p">))</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># P(X) = N(1, 4)</span>
            <span class="n">rep_str</span> <span class="o">=</span> <span class="s2">&quot;P(</span><span class="si">{X}</span><span class="s2">) = N(</span><span class="si">{beta_0}</span><span class="s2">; </span><span class="si">{variance}</span><span class="s2">)&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                <span class="n">X</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">variable</span><span class="p">),</span>
                <span class="n">beta_0</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">beta_0</span><span class="p">),</span>
                <span class="n">variance</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">variance</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">rep_str</span></div>
</pre></div>

          </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../../index.html">pgmpy 0.1.2 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../../index.html" >Module code</a> &#187;</li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2016, Ankur Ankan.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.5.1.
    </div>
  </body>
</html>